package rabbitmq.routing;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DeliverCallback;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
import rabbitmq.common.Const;

/**
 * @author zss
 * @date 2021/8/27 17:34
 * @Email:451682479@qq.com
 */
public class RoutingConsumer2 {

    private static final String EXCHANGE_NAME = "logs2";

    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(Const.host);
        factory.setUsername(Const.username);
        factory.setPassword(Const.password);
        factory.setVirtualHost(Const.VIRTUAL_HOST);

        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        String queue = channel.queueDeclare().getQueue();

        /**
         * https://www.rabbitmq.com/tutorials/tutorial-four-java.html
         * The meaning of a binding key depends on the exchange type.
         * The fanout exchanges, which we used previously, simply ignored its value.
         */

        channel.queueBind(queue, EXCHANGE_NAME, "white");

        DeliverCallback deliverCallback = (consumerTag, delivery) -> {
            String message = new String(delivery.getBody(), "UTF-8");

            System.out.println(" white Received '" + message + "'");

            //channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
        };
        boolean autoAck = false; // acknowledgment is covered below
        channel.basicConsume(queue, autoAck, deliverCallback, consumerTag -> {
        });
    }
}
